<html>

<!-- Mirrored from bos.asmhackers.net/docs/booting/docs/tutorial1.htm by HTTrack Website Copier/3.x [XR&CO'2013], Tue, 07 Oct 2014 11:21:54 GMT -->
<head>

</head>
<body>
	<h1>The booting process</h1><br>

	<h2 class="header">Things that are good to know...</h2>

	<h2>So, you want to make your own OS, but has no clue where to start?! First, you need to find the right tools. I use mixed assembly and C-code to make MuOS. The tools I use are:<br><br>

	<ul><li>For assembly: <b><a href="http://nasm.sourceforge.net/" target="_blank">Nasm</a></b></li>
	<li>For C-code: <b><a href="http://gcc.gnu.org/" target="_blank">Gcc</a></b> (I use <b><a href="http://www.delorie.com/djgpp/" target="_blank">Djgpp</a></b>, but that's because I do my work on the Win32-platform, although, everything <i>should</i> work on any platform)</li>
	<li>For PC-emulation: <b><a href="http://bochs.sourceforge.net/" target="_blank">Bochs</a></b> (You don't want to reboot every time you want to test something)</li></ul><br>

	Now, on to the good stuff =). The aim for our OS is to be 32-bit. For those who now thinks: '<i>Hey?! Isn't every 386+, 32-bits processors? Why aim lower that a 386?</i>': - There's a reason!<br><br>

	Back when the 386 was introduced, the 32-bits were a brand new feature. For all thoses people who spent many $$$ for their 16-bit programs, backwards compability was a must. Intel decided to make the 386 start in 16-bit mode and if the Operating System supported 32-bits, it would have to change from 16-bit mode to 32-bit mode manually. And everybody lived happily ever after... Okay, present day. Computers still boot in 16-bit compability mode. There are hardly any 16-bit operating systems left (DOS is 16-bit, and there are still users, so I can't say that there are none).<br><br>

	There are a few things that change dramatically when 32-bit mode are activated and it's just because of these reasons that we want to have a 32-bit OS (some of the features can be accessed in 16-bit mode, but it requires some special techniques to be used):<br><br>

	<ul><li><b>Access to 4 GB of memory</b> - At startup, the processor is in a so called <i>Real mode</i> (16-bit). This limits the memory access to about 1 MB. Howevery, in <i>Protected mode</i> (32-bit), memory up to 4 GB(!) can be accessed. That's a lot more than 1 MB =).</li>
	<li><b>Memory protection</b></li> - The Protected mode makes reason for its name in memory protection. Memory can be write-protected so that critical sections cannot be touched (OS parts or other application's data for example).</li>
	<li><b>Multitasking</b> - The processor has built-in support for task switching. Multitasking is not accually parallell processes, but scheduled processing time. This feature saves the state of the registers and loads it with the next task's register values. (This can be done with software, but it's faster in hardware most of the time)</li></ul><br>

	There are more features, but these are the most important ones (if you ask me). That should cover the basics you'll need to know. On to the good stuff - <b>Writing a <i>boot sector</i>!</b><br><br><br></h2>


	<h2 class="header">The fun and dangerous part!</h2>

	<h2>Create a plain text file called 'boot1.asm'. The first thing we should do, is to tell the compiler, we're compiling to 16-bit instructions. Remember, at the start-up, the computer operates in Real mode (16-bit).</h2>

	<h4>[BITS 16]</h4>

	<h2>Then we tell the compiler, where in the memory our program is resident. In Nasm, this is done by the ORG command. '<i>Why does the compiler need to know that?</i>', you ask. Take a look at this piece of code:</h2>

	<h4>[ORG 0x7C00]</h4>
		<h5>mov ax, [label]</h5><br>
	<h4>label:</h4>
		<h5>dw 0</h5>

	<h2>The <i>mov</i> instruction is assembled into '<i>mov ax, 0x7C03</i>', instead of '<i>mov ax, 0x0003</i>'. This had not been necessary if our program was a '<i>normal</i>' application, but now, we're making a <i>boot sector</i>. The number I picked, 0x7C00, is the memory address the BIOS puts the boot sector it finds in, so this is where our programs is resident. This adds the line:</h2>

	<h4>[ORG 0x7C00]</h4>

	<h2>Now we want to write a message on the screen. To make it simple, we use the BIOS interrupts available in Real mode. Int 10h has all the functionallity we need. <i><font color="blue">(Updated section)</i> To make this tutorial a little more exited, we want to print a colorful character and not just in black and white like the BIOS defaults to. To change the video mode, we put our <i>Set video mode</I>-command in register AH and that's 0 in our case. Then we specify a video mode through register AL and I've chosen 10h (for more information, read <a href="http://www-2.cs.cmu.edu/afs/cs/user/ralf/pub/WWW/files.html" target="_blank">here</a>). It specifies a 80x25 screen with colored fonts. Finally we call the interrupt 10h to make all the magic happen!</font></h2>

	<h5 class="blue">mov ah, 0<br>
	mov al, 10h<br>
	int 10h</h5>

	<h2>Still using interrupt 10h and putting 0Eh into the AH register instead, we tell the BIOS that we want to put a single character onto the screen. The BIOS then takes the ASCII value of AL, combineds this with the color information in <font color="red">BL</font> and prints the character. The <font color="red">BH</font> register is used to set a page number, but we're not using this, so just set it to 0 and ignore it. Now our code for this:</h2>

	<h5>mov ah, 0Eh<br>
	mov al, 'A'</h5>
	<h5 class="red">mov bh, 0<br>
	mov bl, 0Eh<br>
	int 10h</h5>

	<h2>The <font color="red">BL</font> register holds, as said before, the color attribute for the character. I didn't explain why I put 0Fh in <font color="red">BL</font>, but it holds the color code for the character. This seems not to be supported by all BIOSes, but give it a try if you want to (doesn't work in Bochs). The color codes are as follows:<br><br>

	<table class="colors" border=1>
	<tr>
		<td><b>Value</b></td><td width="100" align=center><b>Color</b></td>
		<td><b>Value</b></td><td width="100" align=center><b>Color</b></td>
	</tr>
	<tr>
		<td><i>00h</i></td><td class="black">Black</td>
		<td><i>08h</i></td><td class="dark_gray">Dark gray</td>
	</tr>
	<tr>
		<td><i>01h</i></td><td class="blue">Blue</td>
		<td><i>09h</i></td><td class="bright_blue">Bright blue</td>
	</tr>
	<tr>
		<td><i>02h</i></td><td class="green">Green</td>
		<td><i>0Ah</i></td><td class="bright_green">Bright green</td>
	</tr>
	<tr>
		<td><i>03h</i></td><td class="cyan">Cyan</td>
		<td><i>0Bh</i></td><td class="bright_cyan">Bright cyan</td>
	</tr>
	<tr>
		<td><i>04h</i></td><td class="red">Red</td>
		<td><i>0Ch</i></td><td class="pink">Pink</td>
	</tr>
	<tr>
		<td><i>05h</i></td><td class="magenta">Magenta</td>
		<td><i>0Dh</i></td><td class="bright_magenta">Bright magenta</td>
	</tr>
	<tr>
		<td><i>06h</i></td><td class="brown">Brown</td>
		<td><i>0Eh</i></td><td class="yellow">Yellow</td>
	</tr>
	<tr>
		<td><i>07h</i></td><td class="gray">Gray</td>
		<td><i>0Fh</i></td><td class="White">White</td>
	</tr>
	</table><br><br>

	To get more information about BIOS interrupts, there's a complete listing made by Ralf Brown. You'll find his interrupt list <a href="http://www-2.cs.cmu.edu/afs/cs/user/ralf/pub/WWW/files.html" target="_blank">here</a>.<br><br>

	After we displayed our 'A' we just hang... This is done by making a jump to a jump, to a jump, to a jump, etc...:</h2>
	<h4>hang:</h4>
	<h5>jmp hang</h5>

	<h2>To get the BIOS to recognize the file as a valid boot sector, the word at address 0x510 must be set to 55AAh. First we fill up the rest of the file with zeros and then we add our word. This is done by adding the lines:</h2>
	<h4>times 510-($-$$) db 0</h4>
	<h5>dw 0AA55h</h5><br>


	<h2 class="header">Putting it together</h2>

	<h2>Now we have a valid boot sector, but we must only compile it first. To compile it to a plain binary file, which the BIOS can read, we use nasm (or nasmw on windows) with the switches:</h2>

	<h3>nasm -f bin boot1.asm -o a.img</h3>

	<h2>The '<i>-f bin</i>' specifies the format to plain binary. You can choose a different output name, but I chose '<i>a.img</i>', (simulated floppy disk) because it was a logical name =). Now we are all set to test it. Copy the file into Bochs' directory and run it with the boot1.bin as a floppy and we're done!<br><br>

	Here's a screenshot from Bochs, running the tutorial's code:<br>
	<img src="bochs_in_action.html"><br><br>

	<a href="boot1_asm.html">Download</a> the complete source for this tutorial.<br><br>
	<a href="bochsrc.html">Download</a> my example configuration file for Bochs 2.0.2 - Win32.<br><br>
	<a href="bochsrc-2.html">Download</a> my example configuration file for Bochs (paths to the BIOS may have to be changed, if you're using another distributions than Win32 1.4.1).<br><br>

	Any comments, improvments or found errors? Mail me: <a href="mailto:gregor.brunmar@home.se">gregor.brunmar@home.se</a>.</h2>

	<table class="links" width=100%><tr>
		<td align=left>Move back to <a href="../assembly/assembly.html">Assembly tutorial</a></td>
		<td align=right>Move on to <a href="../tutorial_02/tutorial_02.html">tutorial 2</a></td>
	</tr></table><br>
</body>

<!-- Mirrored from bos.asmhackers.net/docs/booting/docs/tutorial1.htm by HTTrack Website Copier/3.x [XR&CO'2013], Tue, 07 Oct 2014 11:21:57 GMT -->
</html>
